function varargout = SimpleKIDPRO(varargin)
% SIMPLEKIDPRO MATLAB code for SimpleKIDPRO.fig
%      SIMPLEKIDPRO, by itself, creates a new SIMPLEKIDPRO or raises the existing
%      singleton*.
%
%      H = SIMPLEKIDPRO returns the handle to a new SIMPLEKIDPRO or the handle to
%      the existing singleton*.
%
%      SIMPLEKIDPRO('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in SIMPLEKIDPRO.M with the given input arguments.
%
%      SIMPLEKIDPRO('Property','Value',...) creates a new SIMPLEKIDPRO or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before SimpleKIDPRO_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to SimpleKIDPRO_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help SimpleKIDPRO

% Last Modified by GUIDE v2.5 05-Oct-2012 11:24:4

%
%   - Michael Eager,   (michael.eager@monash.edu) 
%   - (c) 2012, Monash Biomedical Imaging, Monash University, Australia


% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @SimpleKIDPRO_OpeningFcn, ...
    'gui_OutputFcn',  @SimpleKIDPRO_OutputFcn, ...
    'gui_LayoutFcn',  [] , ...
    'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before SimpleKIDPRO is made visible.
function SimpleKIDPRO_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to SimpleKIDPRO (see VARARGIN)
global filename
global img
global depth
% Choose default command line output for SimpleKIDPRO
handles.output = hObject;
handles.DatasetSelection=0; % new element in handles to show selected datasets
handles.currentProject='';
handles.version = 1.5;
% Update handles structure
guidata(hObject, handles);
[status dlsoutput] = system ('bash ./dcheck ')
if status ~=0
    disp('SimpleKIDPRO: Error DaRIS script failed checks.');
end

% %set(handles.outputbox,'String',[output dlsoutput]);
% listelem =regexp(dlsoutput, '\n', 'split')
% %set(handles.listbox1,'String',listelem)
% elems = regexp(listelem,'(587.\w.\w)','match')
%    % disp(elems)
% %set(handles.dataset_table,'Data',elems)
% disp([cellstr(elems{1}) cellstr(elems{2})])
% ProjIDs=[];length(elems)
% for ii = 1:length(elems)
%     if ischar(elems{ii}) && ~isempty(elems{ii})
%         ProjIDs = [ProjIDs cellstr(elems{ii})]
%     end
% end
% ProjIDs(find(ProjIDs ==''))=[];
% set(handles.popupmenu1,'String',ProjIDs)
% handles.output = hObject;
% [status dlsoutput] = system ('bash ./dls -r 0')
% %set(handles.outputbox,'String',[output dlsoutput]);
% listelem =regexp(dlsoutput, '\n', 'split')
% %set(handles.listbox1,'String',listelem)
% count=1
% table = get(handles.uitable2,'Data');
% for ii=1:length(listelem)
%     if (strfind(listelem{ii},'dataset'))
%         sline = regexp(listelem{ii},'\s+','split')
%         % Sort into proper columns
%         daris_num = regexp(sline{4},'\.','split')
%         table{count,1} = [daris_num{4} '.' daris_num{5} '/' daris_num{6} '/' daris_num{7}];
%         descr = sline{5};
%         length(sline)
%         for jj = 10:length(sline)
%             descr = horzcat(descr,[' '], sline{jj});
%         end
%         table{count,2} = descr;  %Description
%         table{count,3} = sline{9}; %Dim
%         table{count,4} = num2str(str2double(sline{8})/1024.,'%f.0'); %size in MB
%         table{count,5} = sline{6};  %Date
%         table{count,6} = sline{7};  %Time
%         table{count,7} = sline{4}; %Full CID
%
%         count=count+1
%     end
% end
% set(handles.uitable2,'Data',table);


% UIWAIT makes SimpleKIDPRO wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = SimpleKIDPRO_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


%% DaRIS ProjectID popup menu functions

function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global cur
handles.output = hObject;
contents = cellstr(get(hObject,'String')) %returns popupmenu1 contents as cell array
contents{get(hObject,'Value')} %returns selected item from popupmenu1
ProjectID = regexp(contents{get(hObject,'Value')},'(\d+.\d+.\d+)','match')
% [status dlsoutput] = system (['bash ./dls  ' ProjectID])
% if status ==0
%     disp(dlsoutput)
% else
%     disp('SimpleKIDPRO: Error you are not logged into DaRIS.');
%     return
% end
handles.currentProject = char(ProjectID)
guidata(hObject, handles);
UpdateTable(handles)

% %set(handles.outputbox,'String',[output dlsoutput]);
% listelem =regexp(dlsoutput, '\n', 'split')
% %set(handles.listbox1,'String',listelem)
% count=1
% table = get(handles.uitable2,'Data');
% for ii=1:length(listelem)
%     if (strfind(listelem{ii},'dataset'))
%         sline = regexp(listelem{ii},'\s+','split')
%         % Sort into proper columns
%         %table{count,1} = false;   % Selection toggle
%         daris_nums = regexp(sline{4},'\.','split')
%         table{count,1} = [daris_num{4} '.' daris_num{5} '.' daris_num{6} '.' daris_num{7}];
%         table{count,2} = sline{9}; % Dims
%         table{count,3} = sline{8}; % Size kB
%         table{count,4} = sline{6}; %date
%         table{count,5} = sline{7}; %time
%         table{count,6} = strcat(sline{10:end});  %Description
%         table{count,7} = sline{4}; %Full CID
%     end
% end
% set(handles.uitable2,'Data',table)

% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
%UpdateProjects(handles)


%% PIPELINE button
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

display 'Running "Pipeline" ... '
Selection = handles.DatasetSelection
if isempty(Selection) %|| length(Selection) > 3
    disp(' No dataset selected or too many')
    return
else
    table = get(handles.uitable2,'Data');
    curProj =handles.currentProject
    %char(table{Selection(1),1})
    description = char(table{Selection(1),2})
    if ~isempty(strfind(description,'depth'))
        DatasetID = [char(curProj) '.' char(table{Selection(1)-1,1})]
        img = GetImage(DatasetID,'depth');
    elseif ~isempty(strfind(description,'output'))
        DatasetID = [char(curProj) '.' char(table{Selection(1)-2,1})]
        img = GetImage(DatasetID,'output');
    else
        DatasetID = [char(curProj) '.' char(table{Selection(1),1})]
        img = GetImage(DatasetID,'dcm');
    end
    
end

figure;
XglomPipeline(DatasetID)

%
% telapsed = toc(tstart)
% display 'Running Masking: none '
% display 'Running Statistical tests ...'
% addpath('/gpfs/M2Home/projects/Monash016/RatKidney/Agilent');
% [voxels diams labeledgloms] = DepthMeasures(depth);
% save(['.//Cache/' FullCID '/output_01.mat'],'voxels', 'diams', 'labeledgloms')
% telapsed = toc(tstart)
% figure; plot (cumsum(voxels'))
% display 'Pipeline complete for CID '
% CID

% Loop through selected images
%    end
%end
display 'Pipeline complete.'


%% INSPECT button
% Selected line in dataset table is loaded for viewing purposes.
% Raw images are checked to see if they are in local cache or need to be
% retreived from DaRIS.  Depth images are currently stored as MAT files but
% could be stored in Daris in the future.
% Output images
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

display 'Running "Inpect" ... '
Selection = handles.DatasetSelection
if isempty(Selection) %|| length(Selection) > 3
    disp(' No dataset selected or too many')
    return
else
    flag=0
    table = get(handles.uitable2,'Data');
    curProj =handles.currentProject
    %char(table{Selection(1),1})
    description = char(table{Selection(1),2})
    %if ~isempty(strfind(description,'No processed'))
    if ~isempty(strfind(description,'Processed'))
        DatasetID = [char(curProj) '.' char(table{Selection(1)-1,1})]
        img = GetImage(DatasetID,'depth');
        if numel(img)== 1
            flag=1;
        end
    elseif ~isempty(strfind(description,'stats'))
        DatasetID = [char(curProj) '.' char(table{Selection(1)-2,1})]
        img = GetImage(DatasetID,'output');
        if numel(img)<2
            flag=1;
        end
        
        load(['./Cache/' DatasetID '/output_01.mat'])
        PlotXGlomOutputDist(stored_hist,stored_diams,DatasetID);
    else
        DatasetID = [char(curProj) '.' char(table{Selection(1),1})]
        img = GetImage(DatasetID,'dcm');
        if numel(img)< 2
            flag=1;
        end
        
    end
    
    disp(['Inpect retreived image size ; ' num2str(size(img))])
    %figure
    if flag == 0
        if max(img(:)) == 1
            imagesc3(img, 'clim', [0 1], 'colormap',flipud(gray));
        else
            imagesc3(img); %, 'clim', [min(img(:)) max(img(:))]);
        end
    end
end



%% GetImage
% Searches local directory 'Cache' for dataset, otherwise gets it from
% DaRIS.

function imgvol = GetImage(uid,tag)

% Check for dataset in Cache
imgvol=[];
[status lsoutput] =system(['ls ./Cache/' uid])
%     if status ~= 0
%         disp(['Error no Cache directory for ' uid])
%         return
%     end
currentFolder = pwd;
addpath([currentFolder '/plugins/dicom_toolbox_version_2e']);

if ~isempty(strfind(tag,'dcm'))
    [status lsoutput] =system(['ls ./Cache/' uid '/*.dcm'])
    if ~isempty(strfind(lsoutput,'No such file or directory'))
        disp(['Getting dataset ' uid ' from DaRIS.']);
        [status lsoutput] =system(['bash ./dget -c ' uid '  -d ./Cache/ ']);
        [status lsoutput] =system(['ls ./Cache/' uid '/*.dcm']);
        lsoutput = regexprep(lsoutput,'\n','');
        if status ~= 0
            display 'Error in Downloading dataset.'
            return
        end
        if size(lsoutput,1) == 1
            display 'Downloading dataset complete. Reading in.'
            imgvol = dicom_read_volume(lsoutput);
            hReadInDialog=get(0,'CurrentFigure');close(hReadInDialog);
            display 'DICOM loaded.'
            %char(['./Cache/' uid '/0001.dcm']));
            
        else
            display 'Too many dcm files in dataset directory.'
        end
    else
        display 'Dataset in Cache. Loading in.'
        lsoutput = regexprep(lsoutput,'\n','')
        imgvol = dicom_read_volume(lsoutput); %char(['./Cache/' uid '/0001.dcm']));%
        hReadInDialog=get(0,'CurrentFigure');close(hReadInDialog);
        %figure
        pause(1)
        %imagesc(squeeze(single(imgvol(:,:,1,256))))
        
        display ' DICOM loaded.'
        
        
    end
    imgvol = single(imgvol);
    imgvol = squeeze(imgvol);
    disp(['GetImage retreived image mean : ' num2str(mean(imgvol(:)))])
    
    
elseif ~isempty(strfind(tag,'depth'))
    disp(['Getting processed depth ' uid ' from local directory Cache .']);
    [status lsoutput] =system(['ls ./Cache/' uid '/depth*.mat'])
    lsoutput = regexprep(lsoutput,'\n','');
    lsoutput = regexprep(lsoutput,'\n','')
    if status == 0
        display 'Processed depth image in Cache. Loading in.'
        load(lsoutput); imgvol= depth;
        size(imgvol)
        imgvol = single(imgvol);
        imgvol = squeeze(imgvol);
    else
        display 'Error in loading processed images.'
        return %1.;
    end
    
elseif  ~isempty(strfind(tag,'output'))
    disp(['Getting output stats of ' uid ' from local directory Cache.']);
    [status lsoutput] =system(['ls ./Cache/' uid '/output*.mat'])
    lsoutput = regexprep(lsoutput,'\n','');
    lsoutput = regexprep(lsoutput,'\n','')
    if status == 0
        display 'Processed output in Cache. Loading in.'
        load(lsoutput); imgvol=labeledgloms>0;
    else
        display 'Error in loading output stats.'
        return %1.;
    end
    
end

%% Logon button
% Call the DaRIS dialog then update project popup menu.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
display 'Running DaRIS logon  ...'
DarisDialog;

UpdateProjects(handles);
display '         DaRIS logon complete.'


%% CLOSE button
% Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
display 'Closing KidneyProject Application'
close(handles.figure1)

%% Dataset select function
% --- Executes when entered data in editable cell(s) in uitable2.
function uitable2_CellEditCallback(hObject, eventdata, handles)
% hObject    handle to uitable2 (see GCBO)
% eventdata  structure with the following fields (see UITABLE)
%	Indices: row and column indices of the cell(s) edited
%	PreviousData: previous data for the cell(s) edited
%	EditData: string(s) entered by the user
%	NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
%	Error: error string when failed to convert EditData to appropriate value for Data
% handles    structure with handles and user data (see GUIDATA)
display eventdata
selection = eventdata.Indices
table = get(handles.uitable2,'Data')
table{selection(1),1} = ~table{selection(1),1};   % Selection toggle


% --- Executes on selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array
%        contents{get(hObject,'Value')} returns selected ithttp://media.theage.com.au/sport/afl-real-footy/judgement-day-for-judd-3462400.html?from=newsboxem from popupmenu2


% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes when selected cell(s) is changed in uitable2.
function uitable2_CellSelectionCallback(hObject, eventdata, handles)
% hObject    handle to uitable2 (see GCBO)
% eventdata  structure with the following fields (see UITABLE)
%	Indices: row and column indices of the cell(s) currently selecteds
% handles    structure with handles and user data (see GUIDATA)
display eventdata
handles.DatasetSelection = eventdata.Indices
guidata(hObject,handles)
%table = get(handles.uitable2,'Data')
%table{selection(1),1} = ~table{selection(1),1};   % Selection toggle


%% Clean dataset table
function datastrings = FilterDatasets(datastrings,varg)
% Filter cell array 'datastrings' with expressions in 'varg'
if ~isempty(varg)
    if ischar(varg)
        datastrings = regexp(datastrings,['.*' varg '.*'],'match')
        disp(['Filtering ' varg ' from datasets.' ])
    else iscell(varg)
        for ii = 1:length(varg)
            disp(['Filtering ' varg{ii} ' from datasets.' ])
            datastrings = regexp(datastrings,['.*' varg{ii} '.*'],'match')
            %% Clean list
            datastrings(cellfun(@isempty, datastrings))=[];
        end
    end
end
% Remove empty elements
datastrings(cellfun(@isempty, datastrings))=[]

%% Filter out modalities using checkboxes
function outstr = getCheckboxes(handles)
outstr = '';
if get(handles.checkbox1,'Value')
    outstr = horzcat(outstr,['MR ']);
end
if get(handles.checkbox2,'Value')
    outstr = horzcat(outstr,['CT ']);
end
if get(handles.checkbox3,'Value')
    outstr = horzcat(outstr,['OM ']);
end
if get(handles.checkbox4,'Value')
    outstr = horzcat(outstr,['Processed ']);
end
if get(handles.checkbox3,'Value')
    outstr = horzcat(outstr,['Raw ']);
end


%% Update the Project ID popup menu
function UpdateProjects(handles)
display 'Updating Project popup menu ...'
[status dlsoutput] = system ('bash ./dls ')
%set(handles.outputbox,'String',[output dlsoutput]);
listelem =regexp(dlsoutput, '\n', 'split')
listelem(cellfun(@isempty, listelem))=[];
%set(handles.listbox1,'String',listelem)
elems = regexp(listelem,'(\d+.\d+.\d+).*','match')
PIDs={};   % project IDs
count=1;
for i = 1:length(elems)
    if ~isempty(elems{i})
        PIDs{1}(count)=cellstr(elems{i});
        count=count+1;
    end
end
PIDs(cellfun(@isempty, PIDs))=[];
if isempty(PIDs)
    PIDs={'No Projects'}
    curProj=''
else
    curProj=PIDs{1}(1)
end
PIDs{1}
set(handles.popupmenu1,'String',PIDs{1})
display '       done.'


%% Update the dataset table

function UpdateTable(handles,varg)
display 'Updating Tables ...'
handles.output = handles.uitable2;
curProj= handles.currentProject
if isempty(curProj)
    [status dlsoutput] = system ('bash ./dls -r 0 1008.2.18')
else
    [status dlsoutput] = system (['bash ./dls -r 0 ' curProj ])
end
%set(handles.outputbox,'String',[output dlsoutput]);
listelem =regexp(dlsoutput, '\n', 'split');
listelem= regexp(listelem,'.*dataset.*','match'); % Filtering elements with sting 'dataset'
listelem(cellfun(@isempty, listelem))=[];  % Cleaning list
listelem
if length(listelem) == 0
    disp('No datasets in project')
    table{1,1}='No datasets in project';
    set(handles.uitable2,'Data',table);
    return
end

%Daris dls output columns
CIDCOL=4;
MODCOL= 5;
DATECOL=6;
TIMECOL=7;
SIZECOL=8;
DESCRCOL=9;

checkmodes = getCheckboxes(handles)
%set(handles.listbox1,'String',listelem)
count=1;
item_path='';
table = {}; %get(handles.uitable2,'Data');
for ii=1:length(listelem)
    ii
    if ~isempty(strfind(listelem{ii},'dataset'))  % double checking filter
        sline = regexp(listelem{ii},'\s+','split')
        if ~isempty(strfind(checkmodes,'Raw'))
            disp(['Modality: ' sline{1}(MODCOL)]);
            if ~isempty(strfind(checkmodes,char(sline{1}(MODCOL))))
                % Sort into proper columns
                daris_num = regexp(sline{1}(CIDCOL),'\.','split');
                item_path = [char(daris_num{1}(4)) '.' char(daris_num{1}(5)) '.' char(daris_num{1}(6)) '.' char(daris_num{1}(7))]
                table{count,1} = item_path;
                table{count,3} = num2str(int32(str2double(char(sline{1}(SIZECOL)))/(1024.)),'%d'); %size in MB
                descr = char(sline{1}(MODCOL)); % get modality
                length(sline{1});
                if length(sline{1}) >= DESCRCOL
                    for jj = DESCRCOL:length(sline{1})
                        descr = horzcat(descr,' ', char(sline{1}(jj)));
                    end
                    descr
                    table{count,2} = descr;  %Description
                    
                end
                table{count,4} = char(sline{1}(DATECOL));  %Date
                table{count,5} = char(sline{1}(TIMECOL));  %Time
                %table{count,6} = char(sline{1}(CIDCOL)); %Full CID
                %table{count,:}
                count=count+1
            end
        end
        if ~isempty(strfind(checkmodes,'Processed'))
            %             %             %% Search for image in cache, then add extra line in table
            
            curProj = handles.currentProject;
            if ~isempty(curProj)
                [status lsoutput] = system (['ls Cache/' curProj '.' item_path]);
                if status == 0
                    
                    disp([' Found directory for image ' item_path]);
                    
                    [status lsoutput] = system (['ls Cache/' curProj '.' item_path '/depth_mask*.mat']);
                    if status==0
                        table{count,1} = '';
                        table{count,2} = 'Processed data: depth , mask ' ;% lsoutput];
                        count=count+1;
                        [status lsoutput] = system (['ls ./Cache/' curProj '.' item_path '/output*.mat']);
                        %
                        if status==0
                            table{count,1} = '';
                            table{count,2} = 'Output: stats + histograms  ';% lsoutput];
                            count=count+1;
                        end
                    else
                        disp(['Directory found but no processed image ' item_path]);
                        table{count,2} = 'No processed data';
                        count=count+1;
                    end
                else
                    disp([' No directory for image ' item_path]);
                    table{count,2} = 'No processed data';
                    count=count+1;
                    %
                end
                %
            else
                table{count,2} = 'No processed or output data';
                count=count+1;
            end
            %             %             %Reset
            %             %
            %             %             item_path='';
        end
        
    end
end

set(handles.uitable2,'Data',table);
display 'Updating table done.'



% --- Executes on selection change in popupmenu3.
function popupmenu3_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu3 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu3



% --- Executes during object creation, after setting all properties.
function popupmenu3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in togglebutton1.
function togglebutton1_Callback(hObject, eventdata, handles)
% hObject    handle to togglebutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of togglebutton1


% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox1


% --- Executes on button press in checkbox2.
function checkbox2_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox2


% --- Executes on button press in checkbox3.
function checkbox3_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox3


%% Logon -- menu-bar item
function LogonMenu_Callback(hObject, eventdata, handles)
% hObject    handle to LogonMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

DarisDialog;
UpdateProjects(handles);
%UpdateTable(handles);


%% Update ProjectID popupmenu -- menu-bar item
function UpdateProjMenu_Callback(hObject, eventdata, handles)
% hObject    handle to UpdateProjMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
UpdateProjects(handles)


%% Update dataset table -- menu-bar item
function UpdateTablesMenu_Callback(hObject, eventdata, handles)
% hObject    handle to UpdateTablesMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
UpdateTable(handles);



function GetDataFromDaRIS(CID)
if ischar(CID) && ~isempty(CID)
    display 'CID is empty or not a string'
    return
end
[status output] = system (['bash ./dget -c ' CID ' -d ./Cache/ '])
if status==0
    display 'Dataset retreived'
    
else
    disp(['Unable to retreive image ' CID])
    table{count,2} = 'No processed data';
end


% --- Executes on button press in checkbox4.
function checkbox4_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox4


% --- Executes on button press in checkbox6.
function checkbox6_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox6


% --- Executes during object creation, after setting all properties.
function text5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to text5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
handles.version=1.5;
set(hObject,'String', ['Xglom ' num2str(handles.version,'%1.1f') ' Kidney Image Toolkit'])
guidata(hObject, handles);
